﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Common;

namespace CommunitySystem.PageView.AdvancedCustomerQuery
{
    public partial class ColumnWindow : ChildWindow
    {
        private CustomerQuery.CustomerQueryClient client = null;
        private CustomerQuery.Query_ColumnCollection _entity = null;
        public ColumnWindow(int id)
        {
            InitializeComponent();
            client = new CustomerQuery.CustomerQueryClient();
            cbTable.ItemsSource = CustomerQueryColumnTable.GetTypeList();
            cbType.ItemsSource = CustomerQueryColumnType.GetTypeList();
            client.AddQuery_ColumnCollectionCompleted += new EventHandler<CustomerQuery.AddQuery_ColumnCollectionCompletedEventArgs>(client_AddQuery_ColumnCollectionCompleted);
            client.GetQuery_ColumnCollectionCompleted += new EventHandler<CustomerQuery.GetQuery_ColumnCollectionCompletedEventArgs>(client_GetQuery_ColumnCollectionCompleted);
            if (id > 0)
            {
                client.GetQuery_ColumnCollectionAsync("ID=" + id);
            }
            else
            {
                _entity = new CustomerQuery.Query_ColumnCollection();
                this.DataContext = _entity;
            }
        }

        void client_AddQuery_ColumnCollectionCompleted(object sender, CustomerQuery.AddQuery_ColumnCollectionCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功");
                this.DialogResult = true;
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("保存失败");
            }
        }

        void client_GetQuery_ColumnCollectionCompleted(object sender, CustomerQuery.GetQuery_ColumnCollectionCompletedEventArgs e)
        {
            _entity = e.Result.FirstOrDefault();
            this.DataContext = _entity;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (_entity != null)
                client.AddQuery_ColumnCollectionAsync(_entity);
            //this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void cbType_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            //_entity.Type = cbType.SelectedValue.ToString();
        }
    }
}

